# 1．库函数导入 3分
import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import confusion_matrix, classification_report

# 2．数据集的加载 3分
x = np.loadtxt(r'..\..\..\..\..\large_data\机器学习1-周考3-技能\imgX.txt', delimiter=',')
y = np.loadtxt(r'..\..\..\..\..\large_data\机器学习1-周考3-技能\labely.txt', delimiter=',')
m = len(x)

# 3．适当缩放特征3分
scaler = StandardScaler()
x = scaler.fit_transform(x)

# 4．将y值中的10变成0  3分
y[y == 10] = 0

# 5．数据洗牌 3分
np.random.seed(1)
a = np.random.permutation(m)
x = x[a]
y = y[a]

# 6．数据集随机分成训练集和测试集 3分
x_train, x_test, y_train, y_test = train_test_split(x, y, train_size=0.7)

# 7．调用神经网路相关库函数 3分
# 8．调用库函数完成模型的训练和预测，要求训练的最大迭代次数为200    3分
clf = MLPClassifier(hidden_layer_sizes=[100, 64],
                    # activation='relu',  # ATTENTION Need not specify activation='sigmoid', sigmoid is not suitable, the default value will work.
                    alpha=0.1, max_iter=200)
clf.fit(x_train, y_train)
print(f'训练集得分:{clf.score(x_train, y_train)}')

# 9．计算在测试集上的模型得分（准确率）及输出 3分
print(f'测试集上的模型得分（准确率）:{clf.score(x_test, y_test)}')

# 10．打印混淆矩阵和分类报告  3分
h_test = clf.predict(x_test)
print('混淆矩阵:')
print(confusion_matrix(y_test, h_test))
print('分类报告:')
print(classification_report(y_test, h_test))
